<template>
  <div class="main">
     <h3 style="font-size: 24px; font-weight: bold; text-align: center;">修改密码</h3>
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="handleSubmit"
    >
      <a-form-item>
        <a-input-password
          size="large"
          placeholder="请输入新密码"
          v-decorator="[
            'password',
            {rules: [{ required: true, message: '请输入新密码' }], validateTrigger: 'blur'}
          ]"
        >
        <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
        </a-input-password>
      </a-form-item>
      
      <a-form-item>
        <a-input-password
          size="large"
          placeholder="请再次确认密码"
          v-decorator="[
            'confirmPassword',
            {rules: [{ required: true, message: '请再次确认密码' },{ validator: this.checkConfirmPassword }], validateTrigger: 'blur'}
          ]"
        >
          <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
        </a-input-password>
      </a-form-item>
        

      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >确认修改</a-button>
      </a-form-item>
    </a-form>

  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
import { removeToken } from '@/utils/util'
export default {
  components: {
    
  },
  data () {
    return {
      loginBtn: false,
      isLoginError: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false,
        smsSendBtn: false
      }
    }
  },
  created () {
  
  },
  methods: {
    // handler
    handleSubmit (e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        Login
      } = this

      //state.loginBtn = true

      const validateFieldsKey =  ['confirmPassword', 'password']

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          editPassword(values).then((res) => {
            this.loginSuccess(res)
          }).catch((err) => {
            //this.requestFailed(err)
          }).finally(() => {
              state.loginBtn = false
          })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    
    loginSuccess (res) {
      removeToken('pssid')
      this.$router.replace({ path: '/user/login' })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '修改密码成功',
          description:'请重新登录'
        })
      }, 1000)
      this.isLoginError = false
    },
    requestFailed (err) {
      this.isLoginError = true
      this.$notification['error']({
        message: '错误',
        description: err.msg,
        duration: 4
      })
    },
    checkConfirmPassword(rule, value, callback) {
      const password = this.form.getFieldValue('password')
      if (value != password) {
        callback(new Error('确认密码与上一次密码不一样'))
      }
      callback()
    }
  }
}
</script>

<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
